//
//  AppDelegate.m
//  WULIHH
//
//  Created by tang on 16/3/28.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "AppDelegate.h"
#import "Header.h"


#import "UMSocial.h"
#import "UMSocialQQHandler.h"



#import "BaseTabBarViewController.h"
#import "ThreeViewController.h"
#import "FavouriteViewController.h"
#import "SUcaiViewController.h"
#import "PersonCenterViewController.h"

#import "ShowFirstViewController.h"
#import "ViewController.h"
//#import "ic"

#import "Reachability.h"


#import "WuLiHotSearchRequest.h"
#import "WiLiLoginRequest.h"

@interface AppDelegate () <UISplitViewControllerDelegate>{

    Reachability *_reacha;
    Reachability *_localWiFiReach;
    Reachability *_internetConnectionReach;


}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //     Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
//    NSString *string222 = [[NSUserDefaults standardUserDefaults] valueForKey:@"sssss"];
    
    
    // 判断网络状态
    Reachability *reacha = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    _reacha = reacha;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    [_reacha startNotifier];
    
    
    
    NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:userLogin];
    
    if (string == NULL)
    {
//        NSLog(@"111");
        [[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:userLogin];
    }
    else
    {
//        NSLog(@"22222");
        //没有登录
        if (![string isEqualToString:@"yes"])
        {
            [[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:userLogin];
        }
        else
        {
            if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"platform"] isEqualToString:@"qq"])
            {
                [WiLiLoginRequest requestUserLogin:[[NSUserDefaults standardUserDefaults] valueForKey:@"openid"] withUserName:[[NSUserDefaults standardUserDefaults] valueForKey:@"name"] withImage:[[NSUserDefaults standardUserDefaults] valueForKey:@"touxiang"] withPlatForm:@"qq"];
            }
            else
            {
                
            }
        }

    }

    
    
#pragma mark--- 配置友盟
    [UMSocialData setAppKey:@"570b0abae0f55a6f22001138"];
    
    [UMSocialQQHandler setQQWithAppId:@"1105253115" appKey:@"nAyInqTdJYFULtfB" url:@"http://www.umeng.com/social"];
    
    
#pragma mark-----下载资源配置文件
    [self downLoadRescuse];
    
    
    ShowFirstViewController *first = [ShowFirstViewController new];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:first];
    
    FavouriteViewController *fav = [FavouriteViewController new];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:fav];
    
    
    ThreeViewController *threeView = [ThreeViewController new];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:threeView];
    
    
    SUcaiViewController *sucaiView = [SUcaiViewController new];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:sucaiView];
    
    PersonCenterViewController *personView = [PersonCenterViewController new];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:personView];
    
    
    BaseTabBarViewController *tabbar = [[BaseTabBarViewController alloc] init];
    [tabbar setViewControllers:@[nav1, nav2, nav3, nav4, nav5]];
    
    [self.window setRootViewController:tabbar];
    return YES;
}



- (void)downLoadRescuse
{
//下载热门搜索配置问价
    [WuLiHotSearchRequest requestHotRescuse];
    
}



- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSLog(@"--------%@", url.description);
    
    return  [UMSocialSnsService handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"=======%@", url.description);
    return  [UMSocialSnsService handleOpenURL:url];
}

-(void)reachabilityChanged:(NSNotification*)note{
    
    Reachability * reach = [note object];
    
    if (reach == _reacha) {
        // www.baidu.com 可以连通
        if ([reach isReachable]) {
//            NSString *temp = [NSString stringWithFormat:@"baidu Notification Says Reachable(%@)",reach.currentReachabilityString];
//            NSLog(@"%@",temp);
        }else{
//            NSString *temp = [NSString stringWithFormat:@"baidu Notification Says Unreachable(%@)", reach.currentReachabilityString];
//            NSLog(@"%@",temp);
        }
    }else if (reach == _localWiFiReach){
        
    //WIFI 状态
        if ([reach isReachable]) {
//            NSString * temp =[NSString stringWithFormat:@"LocalWIFI Notification Says Reachable(%@)", reach.currentReachabilityString];
//            NSLog(@"%@", temp);
        }else{
//            NSString * temp = [NSString stringWithFormat:@"LocalWIFI Notification Says Unreachable(%@)", reach.currentReachabilityString];
//            NSLog(@"%@", temp);
        }
    }else if (reach == _internetConnectionReach){
        // 互联网是否可以连接
       if ([reach isReachable]) {
//            NSString * temp = [NSString stringWithFormat:@"InternetConnection Notification Says Reachable1(%@)", reach.currentReachabilityString];
//            NSLog(@"%@", temp);
        }else{
//            NSString * temp = [NSString stringWithFormat:@"InternetConnection Notification Says Unreachable1(%@)", reach.currentReachabilityString];
//            NSLog(@"%@", temp);
        }
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - Split view


@end
